<template>
  <div class="add">
    <el-dialog
      :title="{ add: '新增企业', edit: '编辑企业' }[mode]"
      width="600px"
      :visible.sync="isShow"
      center
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="90px">
        <el-form-item label="企业编号" prop="eid">
          <el-input v-model.trim="form.eid" />
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input v-model.trim="form.name" />
        </el-form-item>
        <el-form-item label="企业简称" prop="intro">
          <el-input v-model.trim="form.intro" />
        </el-form-item>
        <el-form-item label="企业简介" prop="short_name">
          <el-input v-model.trim="form.short_name" />
        </el-form-item>
        <el-form-item label="企业备注" prop="remark">
          <el-input v-model.trim="form.remark" />
        </el-form-item>
      </el-form>

      <div class="footer">
        <template footer>
          <el-button @click="closeEvent">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </template>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { enterpriseAdd, enterpriseEdit } from '@/api/enterprise.js'
export default {
  data () {
    return {
      mode: 'add',
      isShow: false,
      form: {
        eid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      },
      rules: {
        eid: [{ required: true, message: '必填', trigger: 'change' }],
        name: [{ required: true, message: '必填', trigger: 'change' }],
        short_name: [{ required: true, message: '必填', trigger: 'change' }],
        intro: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    closeEvent () {
      this.isShow = false
      this.$refs.form.resetFields()
      this.form = {
        eid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      }
    },
    submit () {
      this.$refs.form.validate(async res => {
        if (res) {
          // console.log('验证成功')
          if (this.mode === 'add') {
            await enterpriseAdd(this.form)
            this.$message.success('添加成功')
          } else if (this.mode === 'edit') {
            await enterpriseEdit(this.form)
            this.$message.success('编辑成功')
            console.log(this.form)
          }
          this.closeEvent()
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.add {
  ::v-deep .el-dialog__header {
    background: #109dfa;

    span.el-dialog__title {
      color: #fff;
    }

    i.el-dialog__close.el-icon.el-icon-close {
      color: #fff;
    }
  }

  .footer {
    text-align: center;
    margin-top: 50px;
  }
}
</style>
